package xyz.jpcr.covid19app.Adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import org.jetbrains.annotations.NotNull;

import java.text.NumberFormat;
import java.util.ArrayList;

import xyz.jpcr.covid19app.Model.WorldWiseResults.WorldWiseResults;
import xyz.jpcr.covid19app.Activity.PerCountryData;
import xyz.jpcr.covid19app.R;

public class CountryAdapter extends RecyclerView.Adapter<CountryAdapter.ViewHolder> {

    private Context mContext;
    private ArrayList<WorldWiseResults> arrayList;
    private CountryAdapter.OnItemClickListener mListner;
    public static final String COUNTRY_NAME = "country";
    public static final String COUNTRY_CONFIRMED = "cases";
    public static final String COUNTRY_ACTIVE = "active";
    public static final String COUNTRY_DECEASED = "deaths";
    public static final String COUNTRY_TESTS = "tests";
    public static final String COUNTRY_CIRTICAL = "critical";
    public static final String COUNTRY_RECOVERED = "recovered";
    public static final String COUNTRY_UPDATED = "updated";

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mListner = listener;
    }

    public CountryAdapter(Context mContext, ArrayList<WorldWiseResults> arrayList) {
        this.mContext = mContext;
        this.arrayList = arrayList;
    }

    @NonNull
    @NotNull
    @Override
    public CountryAdapter.ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.activity_country_wise_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull ViewHolder holder, int position) {
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WorldWiseResults clickedItem = arrayList.get(position);
                Intent intent = new Intent(mContext, PerCountryData.class);

                intent.putExtra(COUNTRY_NAME, clickedItem.getCountry());
                intent.putExtra(COUNTRY_CONFIRMED, clickedItem.getCases());
                intent.putExtra(COUNTRY_ACTIVE, clickedItem.getActive());
                intent.putExtra(COUNTRY_CIRTICAL, clickedItem.getCritical());
                intent.putExtra(COUNTRY_RECOVERED, clickedItem.getRecovered());
                intent.putExtra(COUNTRY_DECEASED, clickedItem.getDeaths());
                intent.putExtra(COUNTRY_TESTS, clickedItem.getTests());
                intent.putExtra(COUNTRY_UPDATED, clickedItem.getUpdated());

                mContext.startActivity(intent);
            }
        });

        WorldWiseResults currentItem = arrayList.get(position);
        String countryName = currentItem.getCountry();
        long countryConfirmed = currentItem.getCases();
        String countryFlag = currentItem.getCountryInfo().getFlag();

        holder.country_name_tv.setText(countryName);
        holder.country_confirmed_tv.setText(NumberFormat.getInstance().format(countryConfirmed));
        Glide.with(mContext)
                .load(countryFlag)
                .into(holder.iv);
    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }

    public void filterList(ArrayList<WorldWiseResults> filteredList) {
        arrayList = filteredList;
        notifyDataSetChanged();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView country_name_tv, country_confirmed_tv;
        ImageView iv;
        public ViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);

            country_name_tv = itemView.findViewById(R.id.country_name_textview);
            country_confirmed_tv = itemView.findViewById(R.id.country_confirmed_textview);
            iv = itemView.findViewById(R.id.flag_image);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = getAdapterPosition();
                    if (position != RecyclerView.NO_POSITION) {
                        mListner.onItemClick(position);
                    }
                }
            });
        }
    }

}
